package cn.doctor.assistant.pad.im;

import android.app.Application;
import android.content.Intent;
import android.util.Log;

import com.easemob.EMConnectionListener;
import com.easemob.EMEventListener;
import com.easemob.EMNotifierEvent;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;

/**
 * IM即时通讯相关
 */
public class IMApplication extends Application {

    HXNotifier hxNotifier = null;

    @Override
    public void onCreate() {
        super.onCreate();
        EMChat.getInstance().init(this);
        // 是否Debug模式，Release版最好设置为false，提升性能
        EMChat.getInstance().setDebugMode(true);
        EMChatOptions options = EMChatManager.getInstance().getChatOptions();

        //true表示使用环信的好友体系
        options.setUseRoster(false);

        options.setNotificationEnable(true);
        options.setNoticeBySound(true);
        options.setNoticedByVibrate(false);
        options.setUseSpeaker(true);

        //通知栏提醒
        hxNotifier = new HXNotifier();
        hxNotifier.init(this);
        hxNotifier.setNotificationInfoProvider(new HXNotifier.HXNotificationInfoProvider() {

            @Override
            public String getTitle(EMMessage message) {
                return null;
            }

            @Override
            public int getSmallIcon(EMMessage message) {
                return 0;
            }

            @Override
            public Intent getLaunchIntent(EMMessage message) {
                /*Log.i("jlf", message.toString());
                Intent it = new Intent(UI.getCurrActivity(), SimpleChatActivity.class);
                it.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                it.putExtra("friend", message.getFrom());
//				it.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                return it;*/
                return null;
            }

            @Override
            public String getLatestText(EMMessage message, int fromUsersNum,
                                        int messageNum) {
                return message.getFrom()+":"+ ((TextMessageBody)message.getBody()).getMessage();
            }

            @Override
            public String getDisplayedText(EMMessage message) {
                return null;
            }
        });

        //新消息监听
        EMChatManager.getInstance().registerEventListener(new EMEventListener() {

            @Override
            public void onEvent(EMNotifierEvent event) {
                Log.d("jlf", "event:"+event.getEvent()+","+event.getData());

                EMMessage message = null;
                if(event.getData() instanceof EMMessage){
                    message = (EMMessage)event.getData();
                }

                switch (event.getEvent()) {
                    case EventNewMessage:
                        Log.e("jlf", "收到新消息"+message.getBody());
                        hxNotifier.onNewMsg(message);
                        break;
                    case EventDeliveryAck:
                        message.setDelivered(true);
                        break;
                    case EventReadAck:
                        message.setAcked(true);
                        break;
                    default:
                        break;
                }
            }
        });

        //连接监听
        EMChatManager.getInstance().addConnectionListener(new EMConnectionListener() {
            @Override
            public void onConnected() {
                Log.d("jzsn", "连接成功");
            }

            @Override
            public void onDisconnected(int i) {
                Log.d("jzsn", "连接失败");
            }
        });

    }
}
